<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h1>扫码点餐商家端</h1>
        <p>商家管理系统</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            clearable
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <span>还没有账号？</span>
        <el-link type="primary" @click="goToRegister">立即注册</el-link>
      </div>
    </div>
    
    <div class="decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance } from 'element-plus'
import axios from 'axios'

const router = useRouter()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  const valid = await loginFormRef.value.validate().catch(() => false)
  if (!valid) return
  
  loading.value = true
  
  try {
    const response = await axios.post('/merchants/login', {
      username: loginForm.username,
      password: loginForm.password
    })
    
    const { code, message, data } = response.data
    
    if (code === 200 || code === 0) {
      ElMessage.success(message || '登录成功')
      // 存储用户信息
      if (data && data.token) {
        localStorage.setItem('token', data.token)
      }
      localStorage.setItem('username', loginForm.username)
      
      // 跳转到主页
      router.push('/dashboard')
    } else {
      ElMessage.error(message || '登录失败')
    }
  } catch (error: any) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.message || '登录失败，请检查网络连接')
  } finally {
    loading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  display: flex !important;
  justify-content: center !important;
  align-items: center !important;
  overflow: hidden !important;
  margin: 0 !important;
  padding: 0 !important;
}

.login-box {
  position: relative !important;
  z-index: 2 !important;
  width: 500px !important;
  min-width: 500px !important;
  max-width: 500px !important;
  padding: 60px !important;
  background: rgba(255, 255, 255, 0.95) !important;
  border-radius: 20px !important;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1) !important;
  backdrop-filter: blur(10px) !important;
  margin: 0 !important;
}

.login-header {
  text-align: center !important;
  margin-bottom: 40px !important;
}

.login-header h1 {
  color: #333 !important;
  font-size: 36px !important;
  font-weight: 600 !important;
  margin-bottom: 12px !important;
  line-height: 1.2 !important;
}

.login-header p {
  color: #666 !important;
  font-size: 18px !important;
  margin: 0 !important;
}

.login-form {
  margin-bottom: 30px !important;
  width: 100% !important;
}

.login-form .el-form-item {
  margin-bottom: 28px !important;
  width: 100% !important;
}

.login-form :deep(.el-input) {
  width: 100% !important;
}

.login-form :deep(.el-input__wrapper) {
  height: 50px !important;
  border-radius: 8px !important;
  width: 100% !important;
}

.login-form :deep(.el-input__inner) {
  font-size: 16px !important;
  height: 48px !important;
}

.login-btn {
  width: 100% !important;
  height: 55px !important;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  border-radius: 8px !important;
  font-size: 20px !important;
  font-weight: 500 !important;
  color: white !important;
}

.login-footer {
  text-align: center !important;
  color: #666 !important;
  font-size: 16px !important;
  margin: 0 !important;
}

.decoration {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
  pointer-events: none !important;
}

.circle {
  position: absolute !important;
  border-radius: 50% !important;
  background: rgba(255, 255, 255, 0.1) !important;
  animation: float 6s ease-in-out infinite !important;
}

.circle-1 {
  width: 80px !important;
  height: 80px !important;
  top: 10% !important;
  left: 10% !important;
  animation-delay: 0s !important;
}

.circle-2 {
  width: 120px !important;
  height: 120px !important;
  top: 70% !important;
  right: 10% !important;
  animation-delay: 2s !important;
}

.circle-3 {
  width: 60px !important;
  height: 60px !important;
  top: 40% !important;
  right: 20% !important;
  animation-delay: 4s !important;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-20px);
  }
}
</style> 